package com.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class CoursesDAO extends AbstractDAO  <Courses>{

	private Statement stm = null;
	private PreparedStatement prepstm = null;
	private ResultSet rs = null;
	
	
	@Override
	public void add (Courses entity) {
		
		try
		{
		connection = getConnection();
		prepstm = connection.prepareStatement("INSERT INTO course VALUES (?, ?, ?)");
		prepstm.setInt(1, entity.getId());
		prepstm.setString(2, entity.getName());
		prepstm.setInt(3, entity.getStudent_id());
		prepstm.execute();
		}
		
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
	}

	@Override
	public List <Courses> getAll() {
		
		List <Courses> courseList = new LinkedList <Courses> ();
		
		try {
			connection = getConnection();
			stm = connection.createStatement();
			rs = stm.executeQuery("SELECT * FROM course");
			
			while (rs.next())
			{
				Courses course = new Courses (rs.getInt(1), rs.getString(2), rs.getInt(3));
				courseList.add(course);	
			}
		}
		catch (SQLException e) {	
			e.printStackTrace();
		}
		
		closeConnection();
		return courseList;
	}

	
	@Override
	public void delete(int id) {
	
		try {
			connection = getConnection();
			prepstm = connection.prepareStatement("DELETE * FROM Courses WHERE id = ?");
			prepstm.setInt(1, id);
			prepstm.execute();
			
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	
	@Override
	public Courses getById (int id) throws SQLException {
		
		Courses gr = null;
		
		try
		{
			connection = getConnection();
			prepstm = connection.prepareStatement("SELECT * FROM course WHERE id = ?");
			prepstm.setInt(1, id);
			rs = prepstm.executeQuery();
			
		}
		
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		if (rs.next())
		{
			gr = new Courses (rs.getInt(1), rs.getString(2), rs.getInt(3));
		}
		
		return gr;
	}

	
	
	
	
}
